
import java.io.IOException;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.location.Coordinates;



class TCPConnectionHandler implements Runnable
{
    public void SocketListenerFD(Coordinates coCoordenadas) throws IOException
    {
    	 System.out.println("ENTRE SOCKET");
    	 
    	String IPAddress = "190.18.128.130";
    	
		String name = "socket://" + IPAddress + ":" + 5123;
		StreamConnection sc = (StreamConnection)Connector.open(name,Connector.READ_WRITE);
		
	    Form myFormTCP = new Form("Conexion TCP!");
	    myFormTCP.append( new StringItem(null,"Pase TCP" ));    
	    myFormTCP.addCommand(new Command("Exit", Command.EXIT, 0));
	    myFormTCP.setCommandListener((CommandListener) this);
		
		try{
			OutputStream os = sc.openOutputStream();
		
			//StringBuffer sb = new StringBuffer();
			
			double dbLatitud = coCoordenadas.getLatitude();
			double dbLongitud = coCoordenadas.getLongitude();
			
			String textbox1 = dbLatitud+" - "+dbLongitud;
			
			byte[] data = textbox1.getBytes();
			os.write(data); 
			os.flush();
			os.close();
			
			/////////////**********ESTO ES PARA RECIBIR********************//////////////
			/*
			InputStream is = sc.openInputStream();
			byte[] incoming = new byte[10];
			
			while (is.read(incoming)!= -1) {
			//do something to the byte 
			}			
			if(is !=null){ // This statement may be the problem
			is.close(); // because when i remove the if statement
			is = null; // and just close the stream, an exception occurs
			} // but i do not understand why
			*/
			/*
			os = sc.openOutputStream(); 
			String textbox2;
			byte[] data2 = textbox2.getBytes();
			os.write(data2);			
			os.close(); 
			*/
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
    }
	public void run() {
		// TODO Auto-generated method stub
		
		
	}
}